import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { Scene } from "../Scene";
import { smc } from "../../../common/ecs/SingletonModuleComp";
import { oops } from "db://oops-framework/core/Oops";

/**
 * 战斗结束
 */
@ecs.register('BattleEnd')
export class BattleEndComp extends ecs.Comp {
    isWin: boolean = false;
    reset() {
        this.isWin = false;
    }
}

export class BattleEndSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(BattleEndComp);
    }

    entityEnter(e: Scene): void {
        var bm = e.BattleModel;
        if (e.BattleEnd.isWin) {
            if (bm.xiaoguan < bm.maxXiaoGuan) {
                //添加累计金币
                e.BattleModel.coin += e.BattleModel.table.coin_wave;
                // if (bm.daGuan == 1 && bm.xiaoguan == 3 && smc.account.AccountModel.leader.LeaderModel.level == 1) {
                //     smc.account.MainView.hideGuide();
                //     smc.guide.check(2);
                // } else if (bm.daGuan == 2 && bm.xiaoguan == 1 && smc.account.AccountModel.leader.LeaderModel.level == 2) {
                //     smc.account.MainView.hideGuide();
                //     smc.guide.check(6);
                // } else {
                setTimeout(() => {
                    bm.xiaoguan += 1;
                    e.loadBattleData(3);
                }, 1500 / oops.game.getTimeScale());
                // }
            } else {
                e.BattleModel.coin += e.BattleModel.table.coin_wave;
                var box = 0;
                (bm.table.monsters as Array<Array<Array<number>>>).forEach(monsters => {
                    monsters.forEach(monster => {
                        if (monster[0] == 10001) {
                            box = 1;
                        } else if (monster[0] == 10002) {
                            box = 2;
                        } else if (monster[0] == 10003) {
                            box = 1;
                        } else if (monster[0] == 10004) {
                            box = 2;
                        }
                    });
                });

                var params: any = {
                    level: bm.daGuan,
                    box: box
                }

                let onComplete = {
                    target: this,
                    callback: (data: any) => {
                        bm.xiaoguan = 1;
                        bm.daGuan += 1;
                        bm.daGuanNext += 1;
                        smc.account.addItem("coin", bm.coin);
                        if (box) {
                            smc.account.addItem(`box_lv${box}`, 1);
                        }
                        bm.coin = 0;

                        // smc.account.addItemByServer(data.data.rewards);

                        // if (bm.daGuan == 4 && bm.xiaoguan == 1) {
                        //     smc.account.MainView.hideGuide();
                        //     smc.guide.check(10);
                        // } else if (bm.daGuan == 5 && bm.xiaoguan == 1) {
                        //     smc.account.MainView.hideGuide();
                        //     smc.guide.check(15);
                        // } else {
                            setTimeout(() => {
                                e.loadBattleData(1);
                            }, 1500 / oops.game.getTimeScale());
                       // }

                    }
                }
               // netChannel.game.req("user", "pass", params, onComplete);

               onComplete.callback({data: {rewards: []}});
            }
        } else {
            bm.xiaoguan = 1;
            e.BattleModel.coin = 0;
            e.loadPassFailedPage();
        }

        e.MapModelRole.roles.forEach(element => {
            element.removeShield();
        });
        e.remove(BattleEndComp);
    }
}